import axios from 'axios'
const instance = axios.create({
    baseURL: 'http://localhost:8878',
    // baseURL: '/api',
    timeout: 10000,
})

instance.interceptors.request.use(config => {
    // 在发送请求之前做些处理
    const token = localStorage.getItem('token')
    if (token) {
        config.headers.Authorization = `Bearer ${token}`
        // config.headers.token = `${token}`
    }
    return config
}, error => {
    // 发送请求错误处理
    return Promise.reject(error)
})

instance.interceptors.response.use(response => {
    // 对响应数据做些处理
    return response.data
}, error => {
    // 响应错误处理
    return Promise.reject(error)
})

const api = {
    get(url: string, data?: any):any {
        return instance.get(url, { params: data })
    },
    post(url: string, data: any):any {
        return instance.post(url, data)
    },
    put(url: string, data: any):any{
        return instance.put(url, data)
    },
    delete(url: string, data: any):any {
        return instance.delete(url, { params: data })
    }
}

export default api
